﻿using System.Collections.Generic;
using System.Data;
using System.Text;
using Entity;
using FastReport;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Web;

namespace Web.Class
{
    public class MyReportWeb : MyReport
    {
        #region Property

        /// <summary>
        ///     fastreport对象
        /// </summary>
        public WebReport WebReport;

        #endregion

        #region Contructor

        /// <summary>
        ///     实例化报表
        /// </summary>
        /// <param name="opentype">打开方式(设计,预览,打印)</param>
        /// <param name="csList">获取数据的存储过程参数列表</param>
        /// <param name="printoptions">打印设置(偏移量等)</param>
        public MyReportWeb(eReportOpenType opentype, List<BaoBiaoCS> csList, PrintOptions printoptions)
            : base(opentype, csList, printoptions)
        {
            WebReport = new WebReport();
            WebReport.Report = new Report();
            WebReport.ShowToolbar = true;
            WebReport.AutoHeight = true;
            WebReport.Width = 990;
        }

        public MyReportWeb(eReportOpenType opentype, List<BaoBiaoCS> csList, PrintOptions printoptions,
            WebReport report)
            : base(opentype, csList, printoptions)
        {
            WebReport = report;
            PrintOptions = printoptions;
        }

        #endregion

        #region Method

        /// <summary>
        ///     报表对象注册数据源
        /// </summary>
        /// <param name="dt">数据源</param>
        protected override void RegisterData(DataSet ds)
        {
            for (var i = 0; i < ds.Tables.Count; i++)
            {
                WebReport.Report.RegisterData(ds.Tables[i], "tab" + i);
                WebReport.Report.GetDataSource("tab" + i).Enabled = true;
            }
            WebReport.Report.Prepare();
        }


        /// <summary>
        ///     从数据库数据加载模板
        /// </summary>
        protected override void LoadTemplateFromData()
        {
            WebReport.Report.LoadFromString(Encoding.Default.GetString(BBMB.S_MUBAN));
        }

        public override void LoadXY()
        {
            var x = PrintOptions.X;
            var y = PrintOptions.Y;
            foreach (var o in WebReport.Report.AllObjects)
            {
                if (o.GetType() == typeof(TextObject))
                {
                    var txt = o as TextObject;
                    txt.Left = txt.Left + x; //_options.Offset.X;
                    txt.Top = txt.Top + y; //_options.Offset.Y;
                }
                if (o.GetType() == typeof(BarcodeObject))
                {
                    var bar = o as BarcodeObject;
                    bar.Left = bar.Left + x; //_options.Offset.X;
                    bar.Top = bar.Top + y; //_options.Offset.Y;
                }
                if (o.GetType() == typeof(LineObject))
                {
                    var lin = o as LineObject;
                    lin.Left = lin.Left + x; //_options.Offset.X;
                    lin.Top = lin.Top + y; //_options.Offset.Y;
                }
                if (o.GetType() == typeof(TableObject))
                {
                    var tbo = o as TableObject;
                    tbo.Left = tbo.Left + x; //_options.Offset.X;
                    tbo.Top = tbo.Top + y; //_options.Offset.Y;
                }
            }
        }

        protected override string SaveTemplateToData()
        {
            return WebReport.Report.SaveToString();
        }

        protected override void Design()
        {
            WebReport.Report.Design();
        }

        protected override void Show()
        {
            WebReport.Report.Show(true);
        }

        protected override void Print()
        {
            WebReport.Report.Print();
        }

        protected override void SetOffset()
        {
            if (PrintOptions != null && (!PrintOptions.X.Equals(0) || !PrintOptions.Y.Equals(0)))
                foreach (var o in WebReport.Report.AllObjects)
                {
                    if (o.GetType() == typeof(TextObject))
                    {
                        var txt = o as TextObject;
                        txt.Left = txt.Left + PrintOptions.X;
                        txt.Top = txt.Top + PrintOptions.Y;
                    }
                    if (o.GetType() == typeof(BarcodeObject))
                    {
                        var bar = o as BarcodeObject;
                        bar.Left = bar.Left + PrintOptions.X;
                        bar.Top = bar.Top + PrintOptions.Y;
                    }
                    if (o.GetType() == typeof(LineObject))
                    {
                        var lin = o as LineObject;
                        lin.Left = lin.Left + PrintOptions.X;
                        lin.Top = lin.Top + PrintOptions.Y;
                    }
                }
        }

        public override void Dispose()
        {
            WebReport.Report.Dispose();
        }

        #endregion
    }
}